import {Component, OnInit, ElementRef} from '@angular/core';
import {AlertService} from '../../../shared/alert.service';
import {LoadingService} from '../../../shared/loading.service';
import {WithdrawalsService} from '../withdrawals.service';

@Component({
    selector: 'app-withdrawals-list',
    templateUrl: './withdrawals-list.component.html',
    styleUrls: ['./withdrawals-list.component.scss']
})
export class WithdrawalsListComponent implements OnInit {
    public IMG_URL = IMG_URL;
    public dataArr: Array<any> = [];
    public page: Number = 1;
    public minationId: String;
    public isNodata: Boolean = false;
    public totalItems: any; // 列表数据总数
    public currentPage: any = 1; // 默认的开始页数
    public maxSize: Number = 3; // 显示的最大按钮数量

    public judges: any; // 判断执行的方法
    public modalText: any = ''; // 提示
    public resetOrDel: Boolean = true;
    public dangerModal: Boolean = false;
    public issuccess: Boolean = true;

    public delectindexId: any; // 删除的id
    public keys: any = '';
    public nickname: any = '';
    public phoneNum: any = '';

    public state: any = ''; // 审核状态
    public start_time: any = ''; // 开始时间
    public end_time: any = ''; // 结束时间

    public put_forward_state: any;

    constructor(public service: WithdrawalsService,
                public alert: AlertService,
                public loading: LoadingService,
                public ele: ElementRef) {
    }

    ngOnInit() {
        this.loading.show();
        this.getData();
    }

    keyChanges() {
        if (this.keys == 1) {
            this.nickname = '';
        }
        if (this.keys == 2) {
            this.phoneNum = '';
        }
        if (this.keys == '') {
            this.phoneNum = '';
            this.nickname = '';
        }
    }


    pageChanged(event) {
        this.page = event.page;
        this.loading.show();
        this.getData();
    }

    queryData() {
        this.start_time = this.ele.nativeElement.querySelector('#startime').value;
        this.end_time = this.ele.nativeElement.querySelector('#endtime').value;
    }

    // 获取列表数据
    getData() {
        if (this.start_time > this.end_time) {
            this.alert.show('审核开始时间不能大于审核结束时间');
            return;
        }
        const params: any = {
            nickName: this.nickname,
            pay_state: this.state,
            phoneNum: this.phoneNum,
            pay_type: 2,
            page: this.page,
        };
        console.log(params, '提现');
        this.loading.show();
        this.service.getData(params).subscribe(
            res => {
                console.log(res);
                this.loading.hide();
                if (res['code'] === 0) {
                    if (res['data']['length'] === 0) {
                        this.isNodata = true;
                        this.dataArr = [];
                    } else {
                        this.isNodata = false;
                        this.dataArr = res['data'];
                        this.totalItems = res['count'];
                        for (let i = 0; i < this.dataArr.length; i++) {
                            this.dataArr[i]['add_time'] = this.formatDateTime(this.dataArr[i]['add_time'].substring(0, 10) * 1000);
                        }
                    }
                } else {
                    this.alert.show(res['msg']);
                }
            },
            error => {
                this.alert.show('网络错误');
            }
        );
    }

    delectIndex(id, j) {
        this.minationId = id;
        this.modalText = '你确定要删除?';
        this.judges = j;
        console.log(id);
    }


    hideModal() {
        this.dangerModal = false;
        this.getData();
    }


    exportFroms() {
        const params: any = {
            token: localStorage.token,
            start_time: this.start_time,
            end_time: this.end_time,
            state: this.state,
        };
        console.log(params);
        window.open(ROOT_URL + 'order/withdraw_audit_export?order_number=' + '&token=' + localStorage.token + '&state=' + this.state + '&start_time=' + this.start_time + '&end_time=' + this.end_time);
    }

    // 时间戳转换
    formatDateTime(inputTime) {
        const time = new Date(parseInt(inputTime));
        const y = time.getFullYear();
        const m = time.getMonth() + 1;
        const min = m < 10 ? ('0' + m) : m;
        const d = time.getDate();
        const day = d < 10 ? ('0' + d) : d;
        const h = time.getHours();
        const hours = h < 10 ? ('0' + h) : h;
        const minute = time.getMinutes();
        const second = time.getSeconds();
        const minutes = minute < 10 ? ('0' + minute) : minute;
        const seconds = second < 10 ? ('0' + second) : second;
        return y + '-' + min + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
    };

}
